/*
     "DYNACACHE"
	 Adding dynamic AJAX and caching capability without writing a single line.
	 
    Copyright (C)  2009   Shubhranshu --  shubhranshu.com      project(dot)grid(at)gmail(dot)com
	
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
	
*/

/*
	To Do Notes
	*	Should work for Post data also
	*	May add a variable for event firing selection i.e. onclick or onhover ???
	*	try using browser default storage
	*	incorporate more types like div e.t.c.
	*	Incorporate postload event hookup
	
*/

var linkSelector = '.dychClkLnk';
var targetSelector = '.dychCntr';
var randomMultiplier = 1000000;
var cacheStore = new Array();

$(document).ready(function(){
	var linkList = $(linkSelector);
	var lim = linkList.length;
	for(var i = 0 ; i < lim; i++){
		var linkAttr = linkList[i].href;
		// One more way of implementing a bookmarkable URL decode ang get a bookmarkable url.
		linkList[i].setAttribute('onclick',"javascript:LoadIn('"+escape(linkAttr)+"')");
		linkList[i].setAttribute('href','#'+escape(linkAttr));
	}
	if(window.location.hash.length > 1){
		LoadIn(window.location.hash.substring(1));
	}
});

function LoadIn(loadAddress){
	var loaded = cacheStore[loadAddress];
	if(loaded == null){
		$.get(unescape(loadAddress),
				function(rslt){
					cacheStore[loadAddress] = escape(rslt);
					$(targetSelector)[0].innerHTML = rslt;
				} ,
				'html' 
			 );
	}else{
		$(targetSelector)[0].innerHTML = unescape(cacheStore[loadAddress]);
	}
}

function get_random()
{
    var ranNum= Math.floor(Math.random()*randomMultiplier);
    return ranNum;
}
